package dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import dao.BalanceDao;
import dao.DaoHelper;
import exception.UnknownCustomerException;

public class BalanceDaoImp implements BalanceDao {

	private static BalanceDaoImp balanceDao = new BalanceDaoImp();
	private static DaoHelper daoHelper=DaoHelperImp.getBaseDaoInstance();

	private BalanceDaoImp(){}

	public static BalanceDaoImp getInstance()
	{
		return balanceDao;
	}

	@Override
	public float find(String id) throws UnknownCustomerException {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		float balance = 0;

		try {	
			stmt=con.prepareStatement("select balance from user where id = ?");
			stmt.setString(1, id);
			result=stmt.executeQuery();
			if (result.next()) {
				balance = result.getFloat(1);
			} else {
				throw new UnknownCustomerException();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}
		return balance;

	}

}
